package com.ktjy.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ktjy.dto.DictDataDTO;
import com.ktjy.dto.UserDTO;
import com.ktjy.pojo.DictData;
import com.ktjy.pojo.SysUser;
import com.ktjy.service.IDictDataService;
import com.ktjy.utils.AjaxResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 字典数据表 前端控制器
 * </p>
 *
 * @author author
 * @since 2025-08-20
 */
@RestController
@RequestMapping("/api/sys/dict")
@Tag(name = "字典数据模块")
public class DictDataController {

    @Autowired
    private IDictDataService dictDataService;

    @GetMapping("/list")
    @Operation(summary = "字典列表接口")
    @PreAuthorize("@ss.hasPerm('system:dict:list')")
    public AjaxResult list(DictDataDTO dictDataDTO,
                           @Parameter(description = "页码数") @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                           @Parameter(description = "每页显示条数") @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        IPage<DictData> page = dictDataService.findPage(dictDataDTO, pageNo, pageSize);
        return AjaxResult.success(page);
    }

    //根据字典类型查询字典数据
    @GetMapping("/getDictData")
    @Operation(summary = "字典数据接口")
    @PreAuthorize("@ss.hasPerm('system:dict:query')")
    public AjaxResult getDictData(DictDataDTO dictDataDTO) {
        List<DictData> list = dictDataService.getDictData(dictDataDTO);
        if (list == null) {
            return AjaxResult.error("未查询到字典数据");
        }
        return AjaxResult.success("查询成功", list);
    }

    //    根据字典编码查询字典数据

    @GetMapping("/view/{dictCode}")
    @Operation(summary = "查询字典接口")
    @PreAuthorize("@ss.hasPerm('system:dict:query')")
    public AjaxResult view(@PathVariable("dictCode") Long dictCode) {
        DictData dictData = dictDataService.getById(dictCode);
        if (dictData != null) {
            return AjaxResult.success(dictData);
        }
        return AjaxResult.error("未查询到字典数据");
    }

    // 添加字典数据
    @PostMapping("/add")
    @Operation(summary = "添加字典接口")
    @PreAuthorize("@ss.hasPerm('system:dict:add')")
    public AjaxResult add(@RequestBody DictData dictData) {
        boolean save = dictDataService.save(dictData);
        return save ? AjaxResult.success() : AjaxResult.error("添加失败");
    }

    @PutMapping("/update")
    @Operation(summary = "修改字典接口")
    @PreAuthorize("@ss.hasPerm('system:dict:edit')")
    public AjaxResult update(@RequestBody DictData dictData) {
        boolean update = dictDataService.updateById(dictData);
        return update ? AjaxResult.success() : AjaxResult.error("修改失败");
    }

    @DeleteMapping("/del/{id}")
    @Operation(summary = "删除字典接口")
    @PreAuthorize("@ss.hasPerm('system:dict:remove')")
    public AjaxResult del(@PathVariable Long id) {
        boolean remove = dictDataService.removeById(id);
        return remove ? AjaxResult.success() : AjaxResult.error("删除失败");
    }

    //根据字典类型查询数量
    @GetMapping("/count/{dictType}")
    @Operation(summary = "查询字典数量接口")
    @PreAuthorize("@ss.hasPerm('system:dict:query')")
    public AjaxResult count(@PathVariable("dictType") String dictType) {
        long count = dictDataService.count(new QueryWrapper<DictData>().eq("dict_type", dictType));
        return AjaxResult.success("查询成功", count);
    }
}
